package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.chargeshop;
import yxy.cherry.data.bean.privilege;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.active.module.subordinateclass.PrerogativeGoods;
import yxy.game.pm2.active.module.subordinateclass.PrerogativeLockInfo;
import yxy.game.pm2.bean.Privilege;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ActiveModule_特权商城 extends PlayerModule {

	// bean ---------------------------------
	public static final int acttype = 110;
	
	private HashMap<Integer, PrerogativeGoods> pgmap = new HashMap<Integer, PrerogativeGoods>();
	private HashMap<Integer, PrerogativeLockInfo> lockinfos = new HashMap<Integer, PrerogativeLockInfo>();

	public HashMap<Integer, PrerogativeGoods> getPgmap() {
		return pgmap;
	}
	public void setPgmap(HashMap<Integer, PrerogativeGoods> pgmap) {
		this.pgmap = pgmap;
	}
	public HashMap<Integer, PrerogativeLockInfo> getLockinfos() {
		return lockinfos;
	}
	public void setLockinfos(HashMap<Integer, PrerogativeLockInfo> lockinfos) {
		this.lockinfos = lockinfos;
	}

	// func -----------------------------------
	public static final int nolimittype = -1; // 不限购
	public static final int weeklimittype = 1; // 周限购
	public static final int monthlimittype = 2; // 月限购
	public static final int foreverlimittype = 3; // 永久限购

	/**
	 * 初始化
	 * 
	 * @param
	 * @return
	 */
	private static ActiveModule_特权商城 create(String playerCode) {
		ActiveModule_特权商城 am = new ActiveModule_特权商城();
		HashMap<Integer, PrerogativeGoods> pgmap = new HashMap<Integer, PrerogativeGoods>();
		am.setPlayerCode(playerCode);
		am.setPgmap(pgmap);
		am.set();
		return am;
	}

	public int getPdid(int goodid) {
		Map<String, chargeshop> csmap = DataCenter.getAllData(chargeshop.class);
		ArrayList<String> csmapkeys = new ArrayList<String>(csmap.keySet());
		
		for (int i = 0; i < csmapkeys.size(); i++) {
			String key = csmapkeys.get(i);
			chargeshop cs = csmap.get(key);
			if(cs.getPurchasetype() == 1) {
				if(cs.getPurchasever() == goodid) {
					return cs.getPrivilegeid();
				}
			}
		}
		return 0;
	}
 	
	/**
	 * 锁单
	 * @param goodid
	 * @param myorderid
	 */
	public void lock(int goodid, String myorderid) {
		int pdid = getPdid(goodid);
		if(pdid == 0) {
			return;
		}
		
		PrerogativeLockInfo info = new PrerogativeLockInfo();
		info.setPdid(pdid);
		info.setMyorderid(myorderid);
		info.setLocktime(DateTime.now());

		getLockinfos().put(pdid, info);
		set();
	}

	/**
	 * 解锁
	 * @param goodid
	 * @param myorderid
	 */
	public void unlock(int goodid, String myorderid) {
		int pdid = getPdid(goodid);
		if(pdid == 0) {
			return;
		}
		getLockinfos().remove(pdid);
		set();
	}

	/**
	 * 能否购买某个特权商品
	 * 
	 * @param pgid
	 * @return
	 */
	public int canbuy(int pgid) {
		if (getLockinfos().containsKey(pgid)) {
			return DrawStatus.cannotget.getStatus();
		}
		HashMap<Integer, PrerogativeGoods> pgmap = getPgmap();
		if (pgmap == null || pgmap.isEmpty()) {
			return DrawStatus.canget.getStatus();
		}
		if (pgmap.containsKey((Object) pgid)) {
			PrerogativeGoods pg = pgmap.get(pgid);
			DateTime time = pg.getEnd();
			if (DateTime.now().isAfter(time)) {
				return DrawStatus.canget.getStatus();
			} else {
				return DrawStatus.hadget.getStatus();
			}
		}
		return DrawStatus.canget.getStatus();
	}

	/**
	 * 能否购买某个特权商品
	 * 
	 * @param pgid
	 * @return
	 */
	public boolean canbuy2(int pgid) {
		if (getLockinfos().containsKey(pgid)) {
			return false;
		}
		HashMap<Integer, PrerogativeGoods> pgmap = getPgmap();
		if (pgmap == null || pgmap.isEmpty()) {
			return true;
		}
		if (pgmap.containsKey((Object) pgid)) {
			PrerogativeGoods pg = pgmap.get(pgid);
			DateTime time = pg.getEnd();
			if (DateTime.now().isAfter(time)) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获取已有的特权商品id列表
	 * 
	 * @param pcode
	 * @return
	 */
	static ArrayList<Integer> pglist(String pcode) {
		ActiveModule_特权商城 am = ActiveModule_特权商城.get(pcode);
		HashMap<Integer, PrerogativeGoods> pgmap = am.getPgmap();
		if (pgmap == null || pgmap.isEmpty()) {
			return new ArrayList<Integer>();
		}
		ArrayList<Integer> list = new ArrayList<Integer>(pgmap.keySet());
		Collections.sort(list);
		return list;
	}

	/**
	 * 
	 * @param pgid
	 * @param cs
	 */
	public void addprerogative(int pgid, chargeshop cs) {
		HashMap<Integer, PrerogativeGoods> pgmap = getPgmap();

		if (pgmap.containsKey((Object) pgid)) {
			return;
		}

		DateTime now = DateTime.now();

		PrerogativeGoods pg = new PrerogativeGoods();
		pg.setPrivilegeid(cs.getPrivilegeid());
		pg.setPgid(cs.getId());
		pg.setBuytime(now);

		privilege pl = DataCenter.getData(cs.getPrivilegeid(), privilege.class);
		int time = pl.getTime();

		if (time != -1) {
			DateTime start = now.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
			DateTime end = start.plusDays(time);

			pg.setStart(start);
			pg.setEnd(end);
		}

		pgmap.put(pgid, pg);
		setPgmap(pgmap);
		this.set();

		{
			Privilege.add(getPlayerCode(), pg.getPrivilegeid(), pg.getEnd());
		}
	}

	private void update() {
		HashMap<Integer, PrerogativeGoods> pgmap = getPgmap();
		DateTime nowdt = DateTime.now();

		ArrayList<Integer> pgmapkeys = new ArrayList<Integer>(pgmap.keySet());

		HashMap<Integer, DateTime> pdtimemap = new HashMap<Integer, DateTime>();
		for (int i = 0; i < pgmapkeys.size(); i++) {
			int key = pgmapkeys.get(i);
			PrerogativeGoods pg = pgmap.get(key);

			DateTime enddt = pg.getEnd();
			if (enddt == null) {
				continue;
			}

			if (nowdt.isAfter(enddt)) {
				pgmap.remove((Object) key);
				Privilege.remove(getPlayerCode(), pg.getPrivilegeid());
			}
			pdtimemap.put(pg.getPrivilegeid(), pg.getEnd());
		}

		setPgmap(pgmap);
		
		//核对锁单
		ArrayList<Integer> pdids = new ArrayList<Integer>();
		for(Integer pdid : getLockinfos().keySet()) {
			PrerogativeLockInfo info = getLockinfos().get(pdid);
			if(info == null) {
				continue;
			}
			DateTime time = info.getLocktime();
			
			if(DateTime.now().getMillis() - time.getMillis() >= LockOrderInfo.lockTime()) {
				pdids.add(pdid);
			}
		}
		
		for(int pdid : pdids) {
			getLockinfos().remove(pdid);
		}
		
		this.set();
	}

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

	// redis ----------------------------------
	static private String key(String pcode) {
		return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
	}

	public void set() {
		try {
			String key1 = key(getPlayerCode());
			setv(key1, this);
			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_特权商城 get(String playerCode) {
		try {
			ActiveModule_特权商城 module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_特权商城.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_特权商城 activeModule_特权商城 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_特权商城);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	private void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
	}

	// lock==========================================================================


	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
